//
//  HTEmotionModel.swift
//  表情键盘
//
//  Created by whtapple on 4/3/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit

class HTEmotionModel: NSObject, NSCoding{
    var id: String
    //传输用数据
    var chs: String?
    //表情图片 HTEmotionModelManager.path
    var png: String?{
        didSet{
    //根据传进来的 png 拿到全路径
            fullpngPath = HTEmotionModelManager.path + "/" + id + "/" + png!
        }
    }
    //图片在Emoticons.bundle中，所以要全路径
    var fullpngPath: String?
    //十六进制吗
    var code: String?{
        didSet{
            let scanner = NSScanner(string: code!)
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            let salar = UnicodeScalar(result)
            emoji = String(Character(salar))
        }
    }
    //emoji
    var emoji:String?
    //初始化
    init(id: String, dict: [String: String]) {
        self.id = id
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //没有 key调用的方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    //描述
    override var description: String{
        return "\n\t表情模型chs:\(chs),png:\(png),code:\(code)"
    }
    //MARK: -归档和解档
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        chs = aDecoder.decodeObjectForKey("chs") as? String
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        if let p = png{
            fullpngPath = HTEmotionModelManager.path + "/" + id + "/" + p
        }
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")

    }
    //MARK: -归档和解档
//    required init?(coder aDecoder: NSCoder) {
//        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
//        chs = aDecoder.decodeObjectForKey("chs") as? String
//        code = aDecoder.decodeObjectForKey("code") as? String
//        emoji = aDecoder.decodeObjectForKey("emoji") as? String
//        png = aDecoder.decodeObjectForKey("png") as? String
//        //因为全路径保存后，下次再运行，路径会变所以要重新赋值
//        if let p = png {
//            fullpngPath = HTEmotionModelManager.path + "/" + id + "/" + p
//        }
//        
//    }
//    func encodeWithCoder(aCoder: NSCoder) {
//        aCoder.encodeObject(id, forKey: "id")
//        aCoder.encodeObject(chs, forKey: "chs")
//        aCoder.encodeObject(png, forKey: "png")
//        aCoder.encodeObject(code, forKey: "code")
//        aCoder.encodeObject(emoji, forKey: "emoji")
//    }
}
